package cn.net.autocode.platform.design.webclient;

import cn.net.autocode.core.utils.HttpClient;
import cn.net.autocode.core.utils.HttpClientFactory;
import cn.net.autocode.platform.design.handle.AutoCodeHandle;
import cn.net.autocode.platform.design.vo.AppInfo;
import cn.net.autocode.platform.design.vo.DeveloperInfo;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;


@Component
public class AcWebClient {

    private Map<String,String> header;

    private final HttpClient httpClient;


    public AcWebClient() {
        httpClient = HttpClientFactory.create("https://autocode.net.cn/");
    }

    public String pageView(JSONObject param){
        return httpClient.postJson("base/license/pageView",param, this.getHeader(),String.class);
    }

    public JSONObject getAcTableCategory(){
        return httpClient.post("base/license/getAcTableCategory",null,this.getHeader(),JSONObject.class);
    }

    public JSONObject getAcTable(JSONObject param){
        return httpClient.postJson("base/license/getAcTable",param,this.getHeader(),JSONObject.class);
    }


    public JSONObject syncToServer(JSONObject param){
        return httpClient.postJson("base/license/syncToServer",param,this.getHeader(),JSONObject.class);
    }

    public JSONObject getPages(){
        return httpClient.post("base/license/getPages",null,this.getHeader(),JSONObject.class);
    }

    public JSONObject pageRelease(JSONObject param){
        return httpClient.postJson("base/license/pageRelease",param,this.getHeader(),JSONObject.class);
    }

    public JSONArray getReleaseVersion(JSONObject param){
        return httpClient.postJson("base/license/getReleaseVersion",param,this.getHeader(),JSONArray.class);
    }


    public JSONObject delTable(String tableName){
        Map<String,Object> param = new HashMap<>();
        param.put("tableName",tableName);
        return httpClient.get("base/license/delTable",param,this.getHeader(),JSONObject.class);
    }

    public JSONObject licenseLogin(JSONObject param){
        AppInfo appInfo = AutoCodeHandle.getAppInfo();
        if(appInfo==null){
            throw new RuntimeException("没有设置授权信息，请登陆(https://autocode.net.cn)下载license.lic授权文件，下载后复制到resources目录下，重启应用！");
        }

        String appName = AutoCodeHandle.getAppName();

        if (appName != null && !appName.equals(appInfo.getAppName())) {
            throw new RuntimeException("本地项目名称【"+appName+"】与服务端应用代码不一致，修改本地项目名称或登陆(https://autocode.net.cn)获取最新的授权文件！");
        }

        Map<String,String> header = new HashMap<>();
        header.put("appname",appName);
        header.put("license",appInfo.getLicense());
        return httpClient.postJson("base/license/login",param,header,JSONObject.class);
    }

    public String openDesign(JSONObject param){
        return httpClient.get("base/license/openDesign",param,this.getHeader(),String.class);
    }

    public JSONObject assistantChat(JSONObject param){
        return httpClient.postJson("base/license/assistantChat",param,this.getHeader(),JSONObject.class);
    }

    public JSONObject assistantLastChat(JSONObject param){
        return httpClient.postJson("base/license/assistantLastChat",param,this.getHeader(),JSONObject.class);
    }


    private Map<String,String> getHeader(){
        if(this.header==null){
            AppInfo appInfo = AutoCodeHandle.getAppInfo();
            if(appInfo==null){
                throw new RuntimeException("没有设置授权信息，请登陆(https://autocode.net.cn)下载license.lic授权文件，下载后复制到resources目录下，重启应用！");
            }

            String appName = AutoCodeHandle.getAppName();

            if (appName != null && !appName.equals(appInfo.getAppName())){
                throw new RuntimeException("本地项目名称【"+appName+"】与服务端应用代码不一致，修改本地项目名称或登陆(https://autocode.net.cn)获取最新的授权文件！");
            }

            //用户信息
            DeveloperInfo developerInfo = AutoCodeHandle.getDeveloperInfo();
            if(developerInfo==null){
                throw new RuntimeException("没有登陆开发者账号，请先在右上角的【开发者信息】栏中进行登陆！");
            }

            Map<String,String> header = new HashMap<>();
            header.put("appname",appName);
            header.put("token",developerInfo.getToken());
            header.put("license",appInfo.getLicense());
            this.header = header;
        }
        return this.header;
    }


    public void clearHeader(){
        this.header=null;
    }


}
